package com.chookss.home.knowledge;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.entity.DiretoryEntity;
import com.chookss.tiku.view.FlowLayout;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DirectoryDetailActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView common_title_txt;
    @BindView(R.id.tvDirectoryName)
    TextView tvDirectoryName;
    @BindView(R.id.tvDirectoryPeople)
    TextView tvDirectoryPeople;
    @BindView(R.id.tvDirectoryTime)
    TextView tvDirectoryTime;
    @BindView(R.id.tvDirectoryTimeInit)
    TextView tvDirectoryTimeInit;
    @BindView(R.id.tvDirectorySource)
    TextView tvDirectorySource;
    @BindView(R.id.tvDirectorySourceInit)
    TextView tvDirectorySourceInit;
    @BindView(R.id.tvDirectoryKeywordsInit)
    TextView tvDirectoryKeywordsInit;
    @BindView(R.id.tvDirectoryKeywords)
    TextView tvDirectoryKeywords;
    @BindView(R.id.flowLayout2)
    FlowLayout flowLayout2;
    @BindView(R.id.flowLayout)
    FlowLayout flowLayout;
    private List<DiretoryEntity.UpdateRoleBean> updateRoleBeanList= new ArrayList<>();
    private List<DiretoryEntity.ViewRoleBean> viewRoleBeanList = new ArrayList<>();
    private FlowAdapter adapter;
    private FlowAdapter2 adapter2;
    private String catalogCode,catalogType;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diretory_detail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState3();
        common_title_txt.setText("详细信息");
        catalogCode=getIntent().getStringExtra("catalogCode");
        catalogType=getIntent().getStringExtra("catalogType");
        getData();
        getCatalogConfig();
    }
    private void getData() {
        Map<String, String> allmap = new HashMap<>();
        allmap.put("catalogCode", catalogCode);
        allmap.put("catalogType", catalogType);
        MyHttpRequest.getDefault().getRequestCompat(null, Urls.selectCatalogInfoByCode, SignUtils2.makeSignStr(allmap), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                if ("200".equals(jsonObject.getString("code"))) {
                    if(tvDirectoryName==null){
                        return;
                    }
                    JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                    JSONObject catalog = jsonObject1.getJSONObject("catalog");
                    JSONArray keyWords = jsonObject1.getJSONArray("keyWords");
                    tvDirectoryName.setText(catalog.getString("catalogName"));
                    tvDirectoryPeople.setText(catalog.getString("createEmployeeName"));
                    tvDirectoryTime.setText(Utils.getAccurateDate(catalog.getString("createTime")));
                    tvDirectorySource.setText(catalog.getString("fullPath"));
                    if(keyWords.size()==0){
                        tvDirectoryKeywords.setText("无");
                    }else{
                        String wordName="";
                        for (int i = 0; i <keyWords.size() ; i++) {
                            JSONObject wordNames=keyWords.getJSONObject(i);
                            if(wordNames.containsKey("wordName")){
                                if(Utils.isNull(wordName)){
                                    wordName=wordNames.getString("wordName");
                                }else{
                                    wordName=wordName+"、"+wordNames.getString("wordName");
                                }
                            }
                        }
                        tvDirectoryKeywords.setText(wordName);
                    };
                }else{
                    MyToast.show(jsonObject.getString("msg"));
                }
            }
            @Override
            public void onError(String errorMsg) {
            }
        });
    }
    private void getCatalogConfig() {
        Map<String, String> allmap = new HashMap<>();
        allmap.put("catalogCode", catalogCode);
        allmap.put("catalogType", catalogType);
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<DiretoryEntity>>() {
        }.getType(), null, Urls.getCatalogConfig, SignUtils2.makeSignStr(allmap), false, new ObserverCallback<DiretoryEntity>() {
            @Override
            public void onSuccess(DiretoryEntity data) {
                if (data != null) {
                    updateRoleBeanList.addAll(data.getUpdateRole());
                    for (int i = 0; i <updateRoleBeanList.size() ; i++) {
                        DiretoryEntity.ViewRoleBean viewRoleBean=new  DiretoryEntity.ViewRoleBean();
                        viewRoleBean.setRoleName(updateRoleBeanList.get(i).getRoleName());
                        viewRoleBeanList.add(viewRoleBean);
                    }
                    viewRoleBeanList.addAll(data.getViewRole());
                    adapter = new FlowAdapter(DirectoryDetailActivity.this, updateRoleBeanList);
                    flowLayout.setAdapter(adapter);
                    adapter2 = new FlowAdapter2(DirectoryDetailActivity.this, viewRoleBeanList);
                    flowLayout2.setAdapter(adapter2);
                    flowLayout.setVisibility(View.VISIBLE);
                    flowLayout2.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    class FlowAdapter extends FlowLayout.Adapter<FlowAdapter.FlowViewHolder> {

        private static final String TAG = "FlowAdapter";

        private Context mContext;
        private List<DiretoryEntity.UpdateRoleBean> mContentList;

        public FlowAdapter(Context mContext, List<DiretoryEntity.UpdateRoleBean> mContentList) {
            this.mContext = mContext;
            this.mContentList = mContentList;
        }

        @Override
        public FlowAdapter.FlowViewHolder onCreateViewHolder(ViewGroup parent) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_diretory_name, parent, false);
            ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(view.getLayoutParams());
            mlp.setMargins(0, 0, Utils.dip2px(mContext, 12), Utils.dip2px(mContext, 12));
            view.setLayoutParams(mlp);
            return new FlowAdapter.FlowViewHolder(view);
        }

        @Override
        public void onBindViewHolder(FlowAdapter.FlowViewHolder holder, int position) {
            holder.tvTitle.setText(mContentList.get(position).getRoleName());
        }

        @Override
        public int getItemCount() {
            return mContentList.size();
        }

        class FlowViewHolder extends FlowLayout.ViewHolder {
            TextView tvTitle;
            public FlowViewHolder(View itemView) {
                super(itemView);
                tvTitle = itemView.findViewById(R.id.tvTitle);
            }
        }
    }
    class FlowAdapter2 extends FlowLayout.Adapter<FlowAdapter2.FlowViewHolder> {

        private static final String TAG = "FlowAdapter";

        private Context mContext;
        private List<DiretoryEntity.ViewRoleBean> mContentList;

        public FlowAdapter2(Context mContext, List<DiretoryEntity.ViewRoleBean> mContentList) {
            this.mContext = mContext;
            this.mContentList = mContentList;
        }

        @Override
        public FlowAdapter2.FlowViewHolder onCreateViewHolder(ViewGroup parent) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_diretory_name, parent, false);
            ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(view.getLayoutParams());
            mlp.setMargins(0, 0, Utils.dip2px(mContext, 12), Utils.dip2px(mContext, 12));
            view.setLayoutParams(mlp);
            return new  FlowAdapter2.FlowViewHolder(view);
        }

        @Override
        public void onBindViewHolder(FlowAdapter2.FlowViewHolder holder, int position) {
            holder.tvTitle.setText(mContentList.get(position).getRoleName());
        }

        @Override
        public int getItemCount() {
            return mContentList.size();
        }

        class FlowViewHolder extends FlowLayout.ViewHolder {
            TextView tvTitle;
            public FlowViewHolder(View itemView) {
                super(itemView);
                tvTitle = itemView.findViewById(R.id.tvTitle);
            }
        }
    }
    @OnClick(R.id.common_title_back)
    public void onClick() {
        finish();
    }

}
